package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.SignalLampFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.SignalLampHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/8/4.
 */
@Service
public class SignalLampHandleServiceImpl implements SignalLampHandleService {

    @Resource
    private SignalLampFaultRepository signalLampFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getSignalLampFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer lampMaintenanceStatus) throws Exception {
        List<DTLampFaultDto> dto4DTs = signalLampFaultRepository.getSignalLampFaultHandle4DT(dtRequestDto,actorIds, orgId,lampMaintenanceStatus);
        Integer count = signalLampFaultRepository.getSignalLampFaultHandleCount4DT(dtRequestDto,actorIds, orgId,lampMaintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTLampFaultDto dtMachineFaultDto:dto4DTs){
            if(dtMachineFaultDto.getLampMaintainerId()!=null){
                String maintainerName =userRepository.getUserNameByUserId(dtMachineFaultDto.getLampMaintainerId());
                dtMachineFaultDto.setLampMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addLampMaintenanceContent(Integer lampFaultId, String lampMaintenanceContent) throws Exception {
        return signalLampFaultRepository.addLampMaintenanceContent(lampFaultId,lampMaintenanceContent);
    }

    @Transactional
    public Integer finishLampMaintenance(String lampMaintenancePhoto, Integer lampMaintainerId, String lampMaintenanceTime, String lampMaintenanceExplain, Integer lampFaultId) throws Exception {
        return signalLampFaultRepository.finishLampMaintenance(lampMaintenancePhoto,lampMaintainerId,lampMaintenanceTime,lampMaintenanceExplain,lampFaultId);
    }

    public List<SignalLampFaultDto> getLampFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return signalLampFaultRepository.getLampFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public SignalLampFaultDto getLampReceiptInfo4App(Integer lampFaultId) throws Exception{
        return signalLampFaultRepository.getLampReceiptInfo4App(lampFaultId);
    }
}
